<?php

namespace app\index\model;
use think\Model;
use think\model\concern\SoftDelete;
class UserModel extends Model
{
    use SoftDelete;
    protected $name = 'car_user';
//    protected $pk = 'uid';
    protected $autoWriteTimestamp = true;
    protected $dateFormat = 'Y/m/d H:i:s';
    protected $createTime = 'create_at';
    protected $updateTime = 'update_at';
    protected $deleteTime='delete_at';
    // 获取第一个图集 photoList
    public function photoFirst()
    {
        return $this->hasOne(PhotoList::class, 'releaseid', 'id')->order('create_at', 'desc')->limit(1);
    }
    public function photoNum()
    {
        return $this->hasOne(PhotoList::class, 'releaseid', 'id')->where('status', 'SUCCESS');
    }
    public function coll()
    {
        return $this->hasOne(PhotoColl::class, 'userid', 'id');
    }
    public function fans()
    {
        return $this->hasOne(FollowModal::class, 'follow_id', 'id');
    }
    public function concern()
    {
        return $this->hasOne(FollowModal::class, 'userid', 'id');
    }
    /**
     * 检查用户是否存在
     * @param $value
     * @return boolean
     */
    public function checkUser($id) {
        $user = $this->where('id', $id)->findOrEmpty();
        if ($user->isEmpty()) {
            return false;
        }
        return true;
    }
    /**
     * 检查邀请码是否存在
     * @param $value
     * @return boolean
     */
    public function checkInvitationCode($code) {
        $user = $this->where('invitationCode', $code)->findOrEmpty();
        if ($user->isEmpty()) {
            return false;
        }
        return true;
    }
    /**
     * 检查用户是购买图集
     * @param $value
     * @return boolean
     */
    public function checkBuyPhoto($photo_id)
    {
        $buyInfo = $this->hasOne(PhotoBuy::class, 'userid', 'id')->where('photo_id', $photo_id)->findOrEmpty();
        if($buyInfo->isEmpty()) {
            return false;
        }
        return true;
    }

    /**
     * 检查是否关注
     * @param $value
     * @return boolean
     */
    public function checkFollow($userid)
    {
        $buyInfo = $this->hasOne(FollowModal::class, 'follow_id', 'id')->where('userid', $userid)->findOrEmpty();
        if($buyInfo->isEmpty()) {
            return false;
        }
        return true;
    }

    public function checkColl($photo_id)
    {
        $buyInfo = $this->hasOne(PhotoColl::class, 'userid', 'id')->where('photo_id', $photo_id)->findOrEmpty();
        if($buyInfo->isEmpty()) {
            return false;
        }
        return true;
    }

    /**
     * birthday修改位时间戳
     * @param $value
     */
    public function setBirthdayAttr($value)
    {
        return strtotime($value);
    }

    /**
     * birthday获取转成年月日
     * @param $value
     */
    public function getBirthdayAttr($value) {
        return date('Y-m-d', $value);
    }

    public function setShowHomeAttr($value)
    {
        return $value ? 1 : 0;
    }

    public function getShowHomeAttr($value)
    {
        return $value == 1;
    }

//    public function getAvatarAttr($value)
//    {
//        if($value) {
//            return config('app.baseUrl').signUrlTwo($value);
//        }
//        return '';
//    }
}